<?php

namespace Juheip\IpService;

use think\facade\Config as ThinkConfig;

class Config
{
    protected $prefix = 'juheip_platforms_config';

    public function get($key  = null, $default = null)
    {
        // 都框架中用户自定义的配置(位于config/JuheipPlatformsConfig.php)
        $frameworkConfig = ThinkConfig::get($this->prefix);
        // 读扩展包自带的默认配置
        $defaultConfig = require __DIR__ . '/config/juheip_platforms_config.php';
        // 合并配置
        $config = array_merge($defaultConfig, $frameworkConfig ?? []);

        // 按 key 获取配置(空则获取全部配置)
        if (is_null($key)) {

            return $config;
        }
        $keys = explode('.', $key);
        $value = $config;

        foreach ($keys as $k) {
            if (!isset($value [$k])) {

                return $default;
            }
            $value = $value [$k];
        }

        return $value;
    }
}